package com.ply.flashsalessystem.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.entity.pojo.OrderForGoods;
import com.ply.flashsalessystem.entity.pojo.OrderForGoodsDetaile;
import com.ply.flashsalessystem.entity.pojo.Sort;
import com.ply.flashsalessystem.entity.vo.manage.SortVo;
import com.ply.flashsalessystem.entity.wrapper.SortWrapper;
import com.ply.flashsalessystem.exception.OrderException;
import com.ply.flashsalessystem.mapper.SortMapper;
import com.ply.flashsalessystem.service.OrderForGoodsDetaileService;
import com.ply.flashsalessystem.service.OrderForGoodsService;
import com.ply.flashsalessystem.service.SortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ply.flashsalessystem.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@Service
public class SortServiceImpl extends ServiceImpl<SortMapper, Sort> implements SortService {
    @Autowired
    OrderForGoodsDetaileService orderForGoodsDetaileService;

    @Autowired
    OrderForGoodsService orderForGoodsService;

    @Autowired
    SortMapper sortMapper;



    @Override
    public Page<OrderForGoodsDetaile> query(Integer current, Integer limit, Integer goodId, double goodNums) {
       return orderForGoodsDetaileService.queryNumsForSort(current, limit, goodId, goodNums);
    }

    /**
     * @param orgDId 订单详情id
     * @param jrId  入库id
     * @param goodsNums 商品数量
     */
    @Override
    @Transactional
    public boolean sort(Integer orgDId, Integer jrId, double goodsNums) {
        //修改 订单详情表
        if (!orderForGoodsService.updateStatusToPicket(orgDId,goodsNums)) {
            return false;
        }

        // 增加分拣表
        Sort sort = new Sort();
        sort.setJrId(jrId);
        sort.setManageId(UserUtils.getUserId());
        sort.setOrgDId(orgDId);
        sortMapper.insert(sort);
        return true;
    }


    //条件查询 分拣记录
    @Override
    public Page<SortVo> querySort(Integer current, Integer limit, SortWrapper sortWrapper) {
        return sortMapper.querySort(new Page<>(current,limit),sortWrapper);
    }
}
